package com.retrofit.demo;

import android.support.annotation.NonNull;
import android.util.Log;

import com.retrofit.demo.entity.JscbEntiy;
import com.retrofit.demo.entity.YdcdEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * desc:
 * 2018-05-24 11:12
 */
public class HttpUtil {

    public static final String JS_URL = "http://fy.iciba.com/";
    public static final String YD_URL = "http://fanyi.youdao.com/";

    /**
     * 金山词霸请求封装
     */
    public static void request(String baseUrl) {

        // 创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // 创建 网络请求接口 的实例
        Api api = retrofit.create(Api.class);

        switch (baseUrl) {
            case JS_URL:

                // 对 发送请求 进行封装
                Call<JscbEntiy> call = api.getTranslation("ajax.php?a=fy&f=auto&t=auto&w=hello%20world");
                Log.e("JSCB_URL ==>>", call.request().url().toString());
                call.enqueue(new Callback<JscbEntiy>() {
                    @Override
                    public void onResponse(Call<JscbEntiy> call, Response<JscbEntiy> response) {
                        Log.e("HttpUtil==", response.body().toString());
                    }

                    @Override
                    public void onFailure(Call<JscbEntiy> call, Throwable t) {

                    }
                });
                break;
            case YD_URL:
                String path = "translate?doctype=json&jsonversion=&type=&keyfrom=&model=&mid=&imei=&vendor=&screen=&ssid=&network=&abtest=";
                // 对 发送请求 进行封装(设置需要翻译的内容)
                Call<YdcdEntity> call1 = api.traslation(path, "I love you");
                Log.e("YDCD_URL ==>>", call1.request().url().toString());

                call1.enqueue(new Callback<YdcdEntity>() {
                    @Override
                    public void onResponse(@NonNull Call<YdcdEntity> call,
                                           @NonNull Response<YdcdEntity> response) {

                        // 步骤7：处理返回的数据结果：输出翻译的内容
                        YdcdEntity ydcdEntity = response.body();
                        if (ydcdEntity != null) {
                            Log.e("YDETITY ==>", ydcdEntity.toString() + "\n");

                        }
                    }

                    @Override
                    public void onFailure(Call<YdcdEntity> call, Throwable t) {
                        Log.e("YDETITY ==>", t.toString());
                    }
                });

                break;
            default:
                break;
        }


    }


}
